package com.umeox.watch.moto.dataservice.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.base.BaseApp;
import com.umeox.watch.moto.dataservice.services.LocationService;

public class LowBatteryReceiver extends BroadcastReceiver {

    private static final String LAST_UPDATE_TIME = "last_update_time";

    @Override
    public void onReceive(Context context, Intent intent) {
        Logger.i("LowBatteryReceiver->" + intent.getAction());

        if (BuildConfig.FLAVOR.equalsIgnoreCase("Kid_Phone")) {
            long lastUpdateTime = BaseApp.getInstance().getMmkv().getLong(LAST_UPDATE_TIME, 0L);
            if ((System.currentTimeMillis() - lastUpdateTime >= 5 * 60 * 1000) && DataProvider.isActivated()) {
                BaseApp.getInstance().getMmkv().encode(LAST_UPDATE_TIME, System.currentTimeMillis());
                LocationService.lowBatteryLocation(context);
            } else {
                Logger.i("设备未绑定");
            }
        } else {
            //低电定位
            if (DataProvider.isActivated()) {
                LocationService.lowBatteryLocation(context);
            } else {
                Logger.i("设备未绑定");
            }
        }
    }
}